<div class="layui-card-header layui-card">
    <span class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">首页</a>
        <a><cite>支付配置</cite></a>
        <a><cite>支付产品</cite></a>
    </span>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">支付产品列表</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-row">
                    <div class="layui-form" style="float:right;">
                        <div class="layui-form-item" style="margin:0;">
                            <div class="layui-input-inline">
                                <input type="text" name="searchProductName" id="searchProductName" placeholder="产品名称" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-input-inline">
                                <select name="type" id="searchIfMode" placeholder="接口模式" lay-search="">
                                    <option value="-99">接口模式</option>
                                    <option value="1">单独</option>
                                    <option value="2">轮询</option>
                                </select>
                            </div>
                            <div class="layui-input-inline">
                                <select name="type" id="searchStatus" placeholder="状态" lay-search="">
                                    <option value="-99">状态</option>
                                    <option value="0">关闭</option>
                                    <option value="1">开启</option>
                                </select>
                            </div>
                            <a id="search" class="layui-btn" data-type="reload"><i class="layui-icon layui-icon-search"></i>搜索</a>
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layuiAdmin-btns" style="margin-bottom: 10px;">
                        <a class="layui-btn create"><i class="layui-icon layui-icon-add-1"></i>新增支付产品</a>
                    </div>
                </div>
                <table id="cList" lay-filter="cList"></table>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['table','util', 'form', 'admin'], function(){
        var table = layui.table
        ,$ = layui.$
        ,form = layui.form
        ,admin = layui.admin
        ,view = layui.view
        ,element = layui.element;

        element.render('breadcrumb', 'breadcrumb');//渲染导航信息

        //标题内容模板
        var tplTitle = function(d){
            return "<a class='layui-btn layui-btn-xs' lay-event='edit'><i class='layui-icon layui-icon-edit'></i>编辑</a>";
        };

        var tplStatus = function(d){
            var checkedHtml = " id='"+d.id+"' ";
            if(d.status == 1) {
                checkedHtml += " checked";
            }
            return '<input type="checkbox" lay-filter="statusSwitch" lay-skin="switch" '+checkedHtml+' lay-text="启用|停用">';
        };

        var tplProductType = function(d){
            if(d.productType == 1) {
                return '收款';
            }else if(d.productType == 2) {
                return '充值';
            }else {
                return '';
            }
        };

        var tplIfMode = function(d){
            if(d.ifMode == 1) {
                return "单独";
            }else if(d.ifMode == 2) {
                return "<span style='color: red'>轮询</span>";
            }
        };

        var tplMchRate = function(d){
            if(d.mchRate == '' || d.mchRate == null) {
                return "未设置";
            }else {
                return "<span style='color: blue'>"+ d.mchRate+"%<span>";
            }
        };

        var tplAgentRate = function(d){
            if(d.agentRate == '' || d.agentRate == null) {
                return "未设置";
            }else {
                return "<span style='color: blue'>"+ d.agentRate+"%<span>";
            }
        };

        table.render({
            elem: '#cList'
            ,url: layui.setter.baseUrl + '/config/pay_product/list'
            ,where: { access_token: layui.data(layui.setter.tableName).access_token}
            ,id: 'tableReload'
            ,cols: [[
                {field:'id', title: '产品ID'}
                ,{field:'productName', title: '产品名称'}
                ,{field:'productType', title: '产品类型', templet: tplProductType}
                ,{field:'payTypeName',  title: '支付类型'}
                ,{field:'agentRate', width: 150, title: '代理商费率',templet: tplAgentRate}
                ,{field:'mchRate', width: 150, title: '商户费率',templet: tplMchRate}
                ,{field:'ifMode', width: 150,  title: '接口模式',templet: tplIfMode}
                ,{field:'status', title: '状态',templet: tplStatus }
                ,{field:'edite', width: 100, title: '操作', fixed: 'right', templet: tplTitle }
            ]]
            ,page: true
            ,skin: 'line'
        });

        //监听工具条
        table.on('tool(cList)', function(obj){
            var data = obj.data;
            if(obj.event === 'edit'){
                view.xxpayPopup("编辑", "config/pay_product/update", {id: data.id});
            }
        });

        $('.create').click(function(){
            view.xxpayPopup("新增支付产品", "config/pay_product/create", {null: null});
        });

        form.on('switch(statusSwitch)', function(data){

            var id = $(data.elem).attr('id');
            var updateState = data.elem.checked ? "1" : "0";
            var loadingIndex = layer.load();
            admin.req({
                type: "POST",
                url: layui.setter.baseUrl + '/config/pay_product/updateStatus',
                data: {id: id, status: updateState},
                success: function(res){
                    if(res.code == 0) {
                        table.reload('tableReload');
                    }
                },
                complete:function(){
                    layer.close(loadingIndex);
                }
            });
        });
        // 搜索
        var $ = layui.$, active = {
            reload: function(){
                var productName = $('#searchProductName').val();
                var status = $("#searchStatus").val();
                var ifMode = $("#searchIfMode").val();
                //执行重载
                table.reload('tableReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        productName: productName,
                        status: status,
                        ifMode: ifMode
                    }
                });
            }
        };

        $('#search').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
            return false;
        });
        // 渲染表单
        form.render();

    });
    
</script>
